    subroutine run_uhf_s(icore, isympopa, isympopb,& 
                             isymvrta, isymvrtb,      &
                             isymmo, nvmax, nomax, nmo)
!    
     use mod_symm
     use mod_iop
     use mod_orbit
     use mod_size
     use mod_ioff
     implicit none
     integer*8, intent(in) :: nmo, nvmax, nomax, isymmo(nmo, 8, 2)
     integer*8, intent(in) :: isymvrta(nvmax,8), isymvrtb(nvmax,8)
     integer*8, intent(in) :: isympopa(nomax,8), isympopb(nomax,8)
     real*8, intent(in) :: icore(*)
     double precision dtmp
!
     integer*8 maxcor, iintfp, iflags
!
     double precision eccsd, escf, scalfact, tmp
     double precision, allocatable::ea(:), eb(:)
!
     integer*8:: ncycle
!
     integer*8:: nbasis, nsol, nbas2, no2, nv2, nvo, no4, nv2o2
     integer*8:: iread, isox, isoy, isoz, itmp1, itmp2, imoc, norbs
     integer*8:: iaopop(8), irrep, i, j, nsize, idx, ioffa
     integer*8:: irrepab, irrepcd, irrepc, irrepd
!           
     integer*8:: iijkl, iiabj, iabij, iijka, naux
     integer*8:: nv3, nv3o, i0, i1, i2, i3, iend, nvt
     integer*8:: icontl, nabcdab, numab, numcd,numij
     integer*8:: maxcor1, istat, irrepmo(nmo,2), ioffmo(nmo,2)
     integer*8:: c, d, numc, numd, irrepci, numi, irrepi
     integer*8:: numab1, numab2, numab3, numcd1, numcd2, numcd3
     integer*8:: vstart, nfirst, nread, nleft, idx2, nsize2
     double precision tt1, tt2,rt1, rt2, rt3, rt4, rt5, rt6 

     real*8, allocatable :: fijaar(:,:), fijaai(:,:)
     real*8, allocatable :: fijabr(:,:), fijabi(:,:)
     real*8, allocatable :: fiaaar(:,:), fiaaai(:,:)
     real*8, allocatable :: fiaabr(:,:), fiaabi(:,:)
     real*8, allocatable :: fabaar(:,:), fabaai(:,:)
     real*8, allocatable :: fababr(:,:), fababi(:,:)
     real*8, allocatable :: fijr(:,:), fiji(:,:)
     real*8, allocatable :: fiar(:,:), fiai(:,:)
     real*8, allocatable :: fabr(:,:), fabi(:,:)
     real*8, allocatable :: iaam(:), ibbm(:)
     real*8, allocatable :: ea_resort(:), eb_resort(:)
!
     character*12 string2, sfname
     logical :: lsoineom, lscalar, lbdf, lscalsoc
!    
     integer*8 memsize, rec_char, n
     integer*8 fcstar, nbas, nocca, noccb, nirreptmp
     integer*8, external :: idsymsz, irpdso, isymoffso, idsymsoc
     double precision, external :: dnrm2, nrm2so
     real*4, external :: snrm2
!     write(6,*)'begin clmoioso'
     idx = 0  
     do n = 1, nmo 
        do irrep = 1, nirrep
          do i = 1, pop(irrep,1)+vrt(irrep,1)
             if(isymmo(i, irrep,1)==n) then 
                idx = idx + 1
                irrepmo(n,1) = irrep
                ioffmo(n,1) = i 
             endif
          enddo
          do i = 1, pop(irrep,2)+vrt(irrep,2)
             if(isymmo(i, irrep,2)==n) then 
                idx = idx + 1
                irrepmo(n,2) = irrep
                ioffmo(n,2) = i 
             endif
          enddo
        enddo
     enddo
!--------------------------------------------------------------------------
!------for AAAA------------------------------------------------------------

     call cpu_time(tt1)
     call clmoioso0_s(1, icore, pop(1,1), pop(1,1), 0,&
                            pop(1,1), pop(1,1), 0, 'IJKLAAS_')
     call clmoioso0_s(1, icore, pop(1,1), pop(1,1), 0,&
                            pop(1,1), vrt(1,1), 1, 'IJKAAAS_')
     call clmoioso0_s(1, icore, pop(1,1), vrt(1,1), 1,&
                            pop(1,1), pop(1,1), 0, 'KAIJAAS_')
!
!-------------------------------------------------------
     call clmoioso0_s(1, icore, vrt(1,1), pop(1,1), 1,&
                           vrt(1,1), pop(1,1), 1, 'BIAJAAAS')
!----------------------------------------
     call clmoioso0_s(1, icore, vrt(1,1), vrt(1,1), 0,&
                         vrt(1,1), pop(1,1), 1, 'ABCIAAS_')
!--------------------------------------------------
     call clmoioso0_s(1, icore, vrt(1,1), vrt(1,1), 0,&
                          pop(1,1), pop(1,1), 0, 'ABIJAAS_')
!-------------------------------------------------
     call clmoioso0_s(1, icore, pop(1,1), pop(1,1), 0,&
                          pop(1,1), pop(1,1), 0, 'IJKLAAS_')
!    write(6,*)'=================begin resave of abcd================='
     nsize = idsymsoc(1, vrta, vrta, 0, vrta, vrta, 0)
     call clmoioso0_s(1, icore, vrt(1,1), vrt(1,1), 0, &
                         vrt(1,1), vrt(1,1), 0, 'ABCDAAS_')
     call cpu_time(tt2)
     write(6,*)'time for clmoioso', tt2-tt1
!
1000   call cpu_time(tt1)
      i0 = 1
      i1 = i0 + nmo**3
      i2 = i1 + nmo**3
      do i = 1, nmo
         call pyscfio_split('aa_c', 'eri_mo', icore(i1), &
                       (i-1)*nmo+1, nmo, nmo*(nmo+1)/2)
         nirreptmp = nirrep
         call dtos(icore(i1), icore(i2), nmo**3)
         nirrep = 1 
         call iexpso(icore(i2), icore(i0), nmo, nmo, 2, 1,&
                       nmo, zorb, 1, 0, 1)
         nsize = idsymsoc(1, nmo, nmo, 1, nmo, zorb, 1) 
         nirrep = nirreptmp
         call mopy2mo_aaaa_sym_s(icore(i0), icore(i1), nmo, &
                       ioffmo(i,1), irrepmo(i,1), isymmo, isympopa,&
                       isympopb, isymvrta, isymvrtb, nomax, nvmax)
      enddo
!
     call getallso(icore, 1, 'KAIJAAS_') 
     nsize = idsymsoc(1,popa,popa, 0,popa,vrta, 1)
     call itranspso(icore, icore(nsize*isd+1), popa, vrta, 1, &
                 popa, popa, 0, 1)
     call putallso(icore(nsize*isd+1), 1, 'IJKAAAS_') 
     if(ldebug) then 
      call getallso(icore, 1, 'IJKLAAS_') 
      nsize = idsymsoc(1, popa, popa, 0, popa, popa, 0)
      write(6,*)'norm of IJKLAA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'KAIJAAS_') 
      nsize = idsymsoc(1,popa,popa, 0,popa,vrta, 1)
      call itranspso(icore, icore(nsize*isd+1), popa, vrta, 1, &
                  popa, popa, 0, 1)
      call putallso(icore(nsize*isd+1), 1, 'IJKAAAS_') 
      write(6,*)'norm of IJKAAA  ', nrm2so(nsize, icore, 1)
      
      call getallso(icore, 1, 'ABIJAAS_') 
      nsize = idsymsoc(1, vrta, vrta, 0, popa, popa, 0)
      write(6,*)'norm of ABIJAA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'BIAJAAAS') 
      nsize = idsymsoc(1,vrta,popa, 1,vrta,popa, 1)
      write(6,*)'norm of BIAJAAAA', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABCIAAS_')
      nsize = idsymsoc(1,vrta,vrta, 0,vrta,popa, 1)
      write(6,*)'norm of ABCIAA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABCDAAS_') 
      nsize = idsymsoc(1,vrta,vrta, 0,vrta,vrta, 0)
      write(6,*)'norm of ABCDAA  ', nrm2so(nsize, icore, 1)
     endif
!--------------------------------------------------------------------------
!------for BBBB------------------------------------------------------------
     write(6,*)'vrt and pop', nva, nvb, noa, nob
     write(6,*)'vrt and pop', vrt(1,2), pop(1,2)
     call clmoioso0_s(1, icore, pop(1,2), pop(1,2), 0,  &
                               pop(1,2), vrt(1,2), 1, 'IJKABBS_')
     call clmoioso0_s(1, icore, pop(1,2), vrt(1,2), 1,  &
                               pop(1,2), pop(1,2), 0, 'KAIJBBS_')
     call clmoioso0_s(1, icore, vrt(1,2), pop(1,2), 1,  &
                               vrt(1,2), pop(1,2), 1, 'BIAJBBBS')
     call clmoioso0_s(1, icore, vrt(1,2), vrt(1,2), 0,  &
                           pop(1,2), pop(1,2), 0, 'ABIJBBS_')
     call clmoioso0_s(1, icore, pop(1,2), pop(1,2), 0,  &
                           pop(1,2), pop(1,2), 0, 'IJKLBBS_')
     call clmoioso0_s(1, icore, vrt(1,2), vrt(1,2), 0,  &
                             vrt(1,2), pop(1,2), 1, 'ABCIBBS_')
     call clmoioso0_s(1, icore, vrt(1,2), vrt(1,2), 0,  &
                          vrt(1,2), vrt(1,2), 0, 'ABCDBBS_')
      i0 = 1
      i1 = i0 + nmo**3
      i2 = i1 + nmo**3
      do i = 1, nmo
         call pyscfio_split('bb_c', 'eri_mo', icore(i1), &
                       (i-1)*nmo+1, nmo, nmo*(nmo+1)/2)
         nirreptmp = nirrep
         call dtos(icore(i1), icore(i2), nmo**3)
         nirrep = 1
         call iexpso(icore(i2), icore(i0), nmo, nmo, 2, 1,&
                       nmo, zorb, 1, 0, 1)
         nsize = idsymsoc(1, nmo, nmo, 1, nmo, zorb, 1)
         nirrep = nirreptmp
         call mopy2mo_bbbb_sym_s(icore(i0), icore(i1), nmo, &
                       ioffmo(i,2), irrepmo(i,2), isymmo, isympopa,&
                       isympopb, isymvrta, isymvrtb, nomax, nvmax)
      enddo
      call getallso(icore, 1, 'KAIJBBS_')
      nsize = idsymsoc(1,popb,popb, 0,popb,vrtb, 1)
      call itranspso(icore, icore(nsize*isd+1), popb, vrtb, 1, &
                  popb, popb, 0, 1)
      call putallso(icore(nsize*isd+1), 1, 'IJKABBS_')
    if(ldebug) then 
      call getallso(icore, 1, 'IJKLBBS_')
      nsize = idsymsoc(1, popb, popb, 0, popb, popb, 0)
      write(6,*)'norm of IJKLBB  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'KAIJBBS_')
      nsize = idsymsoc(1,popb,popb, 0,popb,vrtb, 1)
      call itranspso(icore, icore(nsize*isd+1), popb, vrtb, 1, &
                  popb, popb, 0, 1)
      call putallso(icore(nsize*isd+1), 1, 'IJKABBS_')
      write(6,*)'norm of IJKABB  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABIJBBS_')
      nsize = idsymsoc(1, vrtb, vrtb, 0, popb, popb, 0)
      write(6,*)'norm of ABIJAA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'BIAJBBBS')
      nsize = idsymsoc(1,vrtb,popb, 1,vrtb,popb, 1)
      write(6,*)'norm of BIAJBBBB', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABCIBBS_')
      nsize = idsymsoc(1,vrtb,vrtb, 0,vrtb,popb, 1)
      write(6,*)'norm of ABCIBB  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABCDBBS_')
      nsize = idsymsoc(1,vrtb,vrtb, 0,vrtb,vrtb, 0)
      write(6,*)'norm of ABCDBB  ', nrm2so(nsize, icore, 1)
   endif 
!--------------------------------------------------------------------------
!------for ABAB------------------------------------------------------------
!    write(6,*)'no and nv', popa 
!    write(6,*)'no and nv', popb
!    write(6,*)'no and nv', vrta 
!    write(6,*)'no and nv', vrtb 
     call clmoioso0_s(1, icore, popa, popb, 1,&
                           popa, popb, 1, 'IJKLABS_')
     call clmoioso0_s(1, icore, pop(1,2), pop(1,1), 1,&
                           pop(1,2), pop(1,1), 1, 'IJKLBAS_')
     call clmoioso0_s(1, icore, pop(1,1), vrt(1,2), 1, &
                             pop(1,1), pop(1,2), 1, 'KAIJABS_')
     call clmoioso0_s(1, icore, pop(1,2), vrt(1,1), 1, &
                             pop(1,1), pop(1,2), 1, 'kAJi_S__')
     call clmoioso0_s(1, icore, pop(1,1), pop(1,2), 1, &
                             pop(1,1), vrt(1,2), 1, 'IJKAABS_')
     call clmoioso0_s(1, icore, pop(1,2), pop(1,1), 1, &
                             pop(1,2), vrt(1,1), 1, 'IJKABAS_')
     call clmoioso0_s(1, icore, vrt(1,1), vrt(1,2), 1,&
                           pop(1,1), pop(1,2), 1, 'ABIJABS_')

     call clmoioso0_s(1, icore, vrt(1,1), pop(1,2), 1,&
                            vrt(1,1), pop(1,2), 1, 'BIAJABAS')
     call clmoioso0_s(1, icore, vrt(1,1), pop(1,1), 1,&
                            vrt(1,2), pop(1,2), 1, 'BIAJAABS')
     call clmoioso0_s(1, icore, vrt(1,2), pop(1,1), 1,&
                            vrt(1,2), pop(1,1), 1, 'BIAJBABS')

     call clmoioso0_s(1, icore, vrtb, popa, 1,&
                           popa,  vrtb, 1, 'BIJABAAS')

     call clmoioso0_s(1, icore, vrt(1,1), vrt(1,2), 1,&
                          vrt(1,1), pop(1,2), 1, 'ABCIABS_')
     call clmoioso0_s(1, icore, vrt(1,2), vrt(1,1), 1,&
                          vrt(1,2), pop(1,1), 1, 'ABCIBAS_')
     call clmoioso0_s(1, icore, vrt(1,2), vrt(1,1), 1,&
                          pop(1,1), vrt(1,2), 1, 'ABICBAAS')


     call clmoioso0_s(1, icore, vrt(1,1), vrt(1,2), 1,&
                          vrt(1,1), vrt(1,2), 1, 'ABCDABS_')

     call cpu_time(tt1)
     if(ldebug) then 
     write(6,*)'finished clmoioso'
     endif
     i0 = 1
     i1 = i0 + nmo**3
     i2 = i1 + nmo**3
!     write(6,*)'pop', pop(:,1), pop(:,2)
     do i = 1, nmo
         call pyscfio_split('ab_c', 'eri_mo', icore(i1), &
                      (i-1)*nmo+1, nmo, nmo*(nmo+1)/2) 
         nirreptmp = nirrep
         call dtos(icore(i1), icore(i2), nmo**3)
         nirrep = 1 
         call iexpso(icore(i2), icore(i0), nmo, nmo, 2, 1,&
                       nmo, zorb, 1, 0, 1)
         nirrep = nirreptmp
!         write(6,*)'nmo', i
!       write(6,*)'norm of tmp', i, snrm2(nmo**3, icore(i0), 1)
!        if(nirrep==1) then 
!         call mopy2mo_abab_s(icore(i0), icore(i1), noa, nob, nva, nvb, nmo, i)
!         elseif(nirrep/=1) then 
         call mopy2mo_abab_sym_s(icore(i0), icore(i1), nmo, &
                       ioffmo(i,2), irrepmo(i,2), isymmo, isympopa,&
                       isympopb, isymvrta, isymvrtb, nomax, nvmax)
!         endif
     enddo
      call getallso(icore, 1, 'BIJABAAS')
      nsize = idsymsoc(1,vrtb,popa, 1,vrtb,popa, 1)
      call isymtrso(icore, icore(nsize*isd+1), vrtb, popa, 1, 0, &
                             popa, vrtb, 1, 1, 1)
      call putallso(icore(nsize*isd+1), 1, 'BIAJBABS')

      call getallso(icore, 1, 'KAIJABS_')
      nsize = idsymsoc(1,popa,popb, 1,popa,vrtb, 1)
      call itranspso(icore, icore(nsize*isd+1), &
                 popa, vrtb, 1, popa, popb, 1, 1)
      call putallso(icore(nsize*isd+1), 1, 'IJKAABS_')

      call getallso(icore, 1, 'kAJi_S__')
      nsize = idsymsoc(1,popb,popa, 1,popb,vrta, 1)
      call itranspso(icore, icore(nsize*isd+1), &
                 popb, vrta, 1, popa, popb, 1, 1)
      call isymtrso(icore(nsize*isd+1), icore, popa, popb, 1, 1, &
                          popb, vrta, 1, 0, 1)
      call putallso(icore, 1, 'IJKABAS_')

      call getallso(icore, 1, 'ABICBAAS')
      nsize = idsymsoc(1,vrtb,vrta, 1,vrtb,popa, 1)
      call isymtrso(icore, icore(nsize*isd+1), vrtb, vrta, 1, 0, &
                     popa, vrtb, 1, 1, 1)
      call putallso(icore(nsize*isd+1), 1, 'ABCIBAS_')
      if(ldebug) then 
      call getallso(icore, 1, 'IJKLABS_')
      nsize = idsymsoc(1,popa,popb, 1,popa,popb, 1)
      write(6,*)'norm of IJKLAB  ', nrm2so(nsize, icore, 1)

!     nsize = idsymsoc(1,popa,popb, 1,popa,vrtb, 1)
!     call getallso(icore, 1, 'IJKAABS_')
!     write(6,*)'norm of IJKAAB  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'KAIJABS_')
      nsize = idsymsoc(1,popa,popb, 1,popa,vrtb, 1)
      call itranspso(icore, icore(nsize*isd+1), &
                 popa, vrtb, 1, popa, popb, 1, 1)
      call putallso(icore(nsize*isd+1), 1, 'IJKAABS_')
      write(6,*)'norm of IJKAAB  ', nrm2so(nsize, icore, 1)

!     nsize = idsymsoc(1,popb,popa, 1,popb,vrta, 1)
!     call getallso(icore, 1, 'IJKABAS_')
!     write(6,*)'norm of IJKABA  ', nrm2so(nsize, icore, 1)
      call getallso(icore, 1, 'kAJi_S__')
      nsize = idsymsoc(1,popb,popa, 1,popb,vrta, 1)
!     call itranspso(icore, icore(nsize*isd+1), &
!                popb, vrta, 1, popb, popa, 1, 1)
!     call isymtrso(icore(nsize*isd+1), icore, popa, popb, 1, 1, &
!                         popb, vrta, 1, 0, 1)
!     call putallso(icore, 1, 'IJKABAS_')
      write(6,*)'norm of IJKABA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABIJABS_')
      nsize = idsymsoc(1,vrta,vrtb, 1,popa,popb, 1)
      write(6,*)'norm of ABIJAB  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'BIAJAABS')
      nsize = idsymsoc(1,vrta,popa, 1,vrtb,popb, 1)
      write(6,*)'norm of BIAJAABB', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'BIAJABAS')
      nsize = idsymsoc(1,vrta,popb, 1,vrta,popb, 1)
      write(6,*)'norm of BIAJABAB', nrm2so(nsize, icore, 1)

!     call getallso(icore, 1, 'BIAJBABS')
!      nsize = idsymsoc(1,vrtb,popa, 1,vrtb,popa, 1)
!     write(6,*)'norm of BIAJBABA', nrm2so(nsize, icore, 1)
      nsize = idsymsoc(1,vrtb,popa, 1,vrtb,popa, 1)
      call getallso(icore, 1, 'BIJABAAS')
      call isymtrso(icore, icore(nsize*isd+1), vrtb, popa, 1, 0, &
                             popa, vrtb, 1, 1, 1)
      call putallso(icore(nsize*isd+1), 1, 'BIAJBABS')
      write(6,*)'norm of BIAJBABA', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABCIABS_')
      nsize = idsymsoc(1,vrta,vrtb, 1,vrta,popb, 1)
      write(6,*)'norm of ABCIAB  ', nrm2so(nsize, icore, 1)

!     call getallso(icore, 1, 'ABCIBAS_')
!     nsize = idsymsoc(1,vrtb,vrta, 1,vrtb,popa, 1)
!     write(6,*)'norm of ABCIBA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABICBAAS')
      nsize = idsymsoc(1,vrtb,vrta, 1,vrtb,popa, 1)
      call isymtrso(icore, icore(nsize*isd+1), vrtb, vrta, 1, 0, &
                     popa, vrtb, 1, 1, 1)
      call putallso(icore(nsize*isd+1), 1, 'ABCIBAS_')
      write(6,*)'norm of ABCIBA  ', nrm2so(nsize, icore, 1)

      call getallso(icore, 1, 'ABCDABS_')
      nsize = idsymsoc(1,vrta,vrtb, 1,vrta,vrtb, 1)
      write(6,*)'norm of ABCDAB  ', nrm2so(nsize, icore, 1)

     endif 

!    call getallso(icore, 1, 'ABCDAA__')
!    nsize = idsymsoc(1,vrta,vrtb,0 ,vrta,vrtb, 0)
!    write(6,*)'norm of ABCDAA  ', dnrm2(nsize, icore, 1)

!    call getallso(icore, 1, 'ABCDAB__')
!    nsize = idsymsoc(1,vrta,vrtb,1 ,vrta,vrtb, 1)
!    write(6,*)'norm of ABCDAB  ', dnrm2(nsize, icore, 1)

!cc--------
!05  write(6,*)'finished cc_inter'
     return
     end



